//
//  AlarmClockManager.m
//  LiveSoftware
//
//  Created by lanou3g on 15/7/22.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "AlarmClockManager.h"

@implementation AlarmClockManager


+(void)addAlarmClockWithFireDate:(NSDate *)date activityId:(NSInteger)aid  activityTitle:(NSString*)title
{
    
    
//    NSDate *time = [NSDate dateWithTimeIntervalSinceNow:0.5*60];
//    NSComparisonResult result = [time  compare:date];
//    if(result ==-1)
//    {
    
        UILocalNotification *notification = [[UILocalNotification alloc]init];
        notification.fireDate = [date dateByAddingTimeInterval:-5*60];
        notification.timeZone = [NSTimeZone defaultTimeZone];
        notification.applicationIconBadgeNumber = -1;
        notification.soundName = UILocalNotificationDefaultSoundName;
        notification.alertBody = [NSString stringWithFormat:@"%@ 将要开始",title];
        
        
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInteger:aid ],@"activityId", nil];
        
        
        notification.userInfo = dic;
        
        [[UIApplication sharedApplication]scheduleLocalNotification:notification];
        
           
//    }
    
    
}

+(BOOL)removeAlarmClockWithActivityId:(NSInteger)aid
{
    
    UIApplication *application = [UIApplication sharedApplication];
    NSArray *localNocations = [[UIApplication sharedApplication]scheduledLocalNotifications];
    
    for(UILocalNotification *obj in  localNocations)
    {
        
        
        NSInteger activityId = [[obj.userInfo  objectForKey:@"activityId"]intValue];
        
        if(aid ==activityId)
        {
            [application cancelLocalNotification:obj];
            return YES;
        }
        
    }
    
    return  NO;
    
}


@end
